Test Series - Data Structure

Test Number 64/115

Q: What is the special property of red-black trees and what root should always be?
A. a color which is either red or black and root should always be black color only
B. height of the tree
C. pointer to next node
D. a color which is either green or black
Solution: An extra attribute which is a color red or black is used. root is black because if it is red then one of red-black tree property which states that number of black nodes from root to null nodes must be same, will be violated.
Q: Why do we impose restrictions like
. root property is black
. every leaf is black
. children of red node are black
. all leaves have same black
A. to get logarithm time complexity
B. to get linear time complexity
C. to get exponential time complexity
D. to get constant time complexity
Solution: We impose such restrictions to achieve self balancing trees with logarithmic complexities for insertions, deletions, search.
Q: What are the operations that could be performed in O(logn) time complexity by red-black tree?
A. insertion, deletion, finding predecessor, successor
B. only insertion
C. only finding predecessor, successor
D. for sorting
Solution: We impose restrictions to achieve logarithm time complexities.
impose restrictions are:
. root property is black
. every leaf is black
. children of red node are black
. all leaves have same black.
Q: Which of the following is an application of Red-black trees and why?
A. used to store strings efficiently
B. used to store integers efficiently
C. can be used in process schedulers, maps, sets
D. for efficient sorting
Solution: RB tree is used for Linux kernel in the form of completely fair scheduler process scheduling algorithm. It is used for faster insertions, retrievals.
Q: When it would be optimal to prefer Red-black trees over AVL trees?
A. when there are more insertions or deletions
B. when more search is needed
C. when tree must be balanced
D. when log(nodes) time complexity is needed
Solution: Though both trees are balanced, when there are more insertions and deletions to make the tree balanced, AVL trees should have more rotations, it would be better to use red-black. but if more search is required AVL trees should be used.
Q: Why Red-black trees are preferred over hash tables though hash tables have constant time complexity?
A. no they are not preferred
B. because of resizing issues of hash table and better ordering in redblack trees
C. because they can be implemented using trees
D. because they are balanced
Solution: Redblack trees have O(logn) for ordering elements in terms of finding first and next elements. also whenever table size increases or decreases in hash table you need to perform rehashing which can be very expensive in real time. also red black stores elements in sorted order rather than input order.
Q: How can you save memory when storing color information in Red-Black tree?
A. using least significant bit of one of the pointers in the node for color information
B. using another array with colors of each node
C. storing color information in the node structure
D. using negative and positive numbering
Solution: The node pointers can be used to store color with the help of significant bits. the exceptions of this method are in languages like java where pointers are not used this may not work.
Q: When to choose Red-Black tree, AVL tree and B-trees?
A. many inserts, many searches and when managing more items respectively
B. many searches, when managing more items respectively and many inserts respectively
C. sorting, sorting and retrieval respectively
D. retrieval, sorting and retrieval respectively
Solution: Red black when frequent inserts and deletes, AVL when less frequent inserts and deletes, B-tree when using paging from a slow storage device.
Q: What is the below pseudo code trying to do, where pt is a node pointer and root pointer?

  redblack(Node root, Node pt) :
    if (root == NULL)
       return pt
 
    if (pt.data < root.data)
    {
        root.left  =   redblack(root.left, pt);
        root.left.parent = root
    }
    else if (pt.data > root.data)
    {
        root.right = redblackt(root.right, pt)
        root.right.parent = root
    }
   return root
A. insert a new node
B. delete a node
C. search a node
D. count the number of nodes
Solution: The code is taking the root node and to be inserted node and is performing insertion operation.

You Have Score    /9